//
//  YHLoadingView.m
//  LoadingDemo
//
//  Created by dllo on 15/11/14.
//  Copyright © 2015年 YHLai. All rights reserved.
//

#import "YHLoadingView.h"
#import "FitHeader.h"

@interface YHLoadingView ()
@property(nonatomic, strong)UIView *superView;
@end

@implementation YHLoadingView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self createViews];
        
        [self changeColor];
        ADD_NSNOTIFICATIONCENTER_OBSERVER;
    }
    return self;
}

- (void)createViews
{
    _backgroundView = [[UIView alloc] init];
//    _backgroundView.backgroundColor = [UIColor whiteColor];
    _backgroundView.layer.cornerRadius = 5;
    [self addSubview:_backgroundView];
    
    _imageView = [[UIImageView alloc] init];
    _imageView.image = [UIImage imageNamed:@"abs__spinner_48_inner_holo"];
    [self createRotationAnimation];
    [self addSubview:_imageView];
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    _backgroundView.frame = CGRectMake(0, 0, 75, 75);
    _backgroundView.center = CGPointMake(self.center.x, self.frame.size.height / 2);
    
    _imageView.frame = CGRectMake(0, 0, 48, 48);
    _imageView.center = _backgroundView.center;
}

- (void)createRotationAnimation
{
    CABasicAnimation *rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
    rotationAnimation.toValue = [NSNumber numberWithInt:M_PI * 2];
    rotationAnimation.duration = 1;
    rotationAnimation.repeatCount = NSIntegerMax;
    [_imageView.layer addAnimation:rotationAnimation forKey:@"rotation"];
}

- (void)changeColor
{
    if (NSUSERDEFAULTS_NIGHT) {
        _backgroundView.backgroundColor = BGCOLOR_LIGHTBLACK;
    } else {
        _backgroundView.backgroundColor = BGCOLOR_WHITE;
    }
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
